package com.atom.process.cnmuseum.cj.render;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.atom.common.RenderStatus;
import com.atom.common.VariablesConstant;
import com.atom.common.remote.user.UserUtil;
import com.atom.process.cnmuseum.cj.entity.task.CjGcxxTaskDO;
import com.atom.process.cnmuseum.cj.tool.CommentDefinitions;
import com.atom.process.cnmuseum.cj.tool.FileDefinitions;
import com.atom.process.cnmuseum.cj.vo.BaseVO;
import com.atom.process.cnmuseum.cj.vo.HtmlVO;
import com.atom.process.cnmuseum.cj.vo.response.ResponseCommentVO;
import com.atom.process.cnmuseum.cj.vo.response.ResponseFileVO;
import com.atom.process.cnmuseum.cj.vo.response.ResponseVO;

@Service
public class CJRender extends AbstractRender implements RenderInterface {

  private static Logger logger = Logger.getLogger(CJRender.class);

  public ResponseVO getDetail(String taskId, UserUtil user) {
    ResponseVO responseVO = new ResponseVO();
    BaseVO vo = new BaseVO();
    ResponseCommentVO comment = new ResponseCommentVO();
    ResponseFileVO file = new ResponseFileVO();
    String processInstanceId = (String) activitiHandler.findVariableByTaskIdByKey(taskId, VariablesConstant.MAIN_PROCESS_INSTANCE_ID);
    // 意见数据
    comment.setShenQingYiJian(
        commentDao.findByTypeByProcessInstanceId(
            CommentDefinitions.申请意见.getType(), processInstanceId));
    comment.setBuMenZhuRenYiJian(
        commentDao.findByTypeByProcessInstanceId(
            CommentDefinitions.部门主任意见.getType(), processInstanceId));
    comment.setZongZhangGuanLiYuanYiJian(
        commentDao.findByTypeByProcessInstanceId(
            CommentDefinitions.总账管理员意见.getType(), processInstanceId));
    responseVO.setComment(comment); 
    // 文件数据
    file.setFuJianList(
        fileDao.findByTypeByProcessInstanceId(
            FileDefinitions.附件.getType(), processInstanceId));
    responseVO.setFile(file);
    // 业务数据
    CjGcxxTaskDO taskDO = cjGcxxTaskDao.findByTaskId(taskId);
    BeanUtils.copyProperties(taskDO, vo);
    /*CjCpdzTaskDO dz = cjCpdzTaskDao.findNewByTaskId(taskId);
    vo.setCangPinFenLei1(dz.getCangPinFenLei1());
    vo.setCangPinFenLei2(dz.getCangPinFenLei2());
    vo.setCangPinFenLei3(dz.getCangPinFenLei3());
    vo.setMoBan(dz.getMoBan());
    vo.setMoBanText(dz.getMoBanText());*/
    responseVO.setVo(vo);
    // 用户数据
    responseVO.setUser(user);      
    return responseVO;
  }

  public HtmlVO getHtmlDetail(String taskId, String processId, UserUtil user, String taskDefKey) {
    HtmlVO vo = new HtmlVO();
    BaseVO base = new BaseVO();
    ResponseCommentVO comment = new ResponseCommentVO();
    ResponseFileVO file = new ResponseFileVO();
    try {
      // 意见数据
      comment.setShenQingYiJian(
          commentDao.findByTypeByProcessInstanceId(
              CommentDefinitions.申请意见.getType(), processId));
      comment.setBuMenZhuRenYiJian(
          commentDao.findByTypeByProcessInstanceId(
              CommentDefinitions.部门主任意见.getType(), processId));
      comment.setZongZhangGuanLiYuanYiJian(
          commentDao.findByTypeByProcessInstanceId(
              CommentDefinitions.总账管理员意见.getType(), processId));
      vo.setComment(comment); 
      // 文件数据
      file.setFuJianList(
          fileDao.findByTypeByProcessInstanceId(
              FileDefinitions.附件.getType(), processId));
      vo.setFile(file);
      // 业务数据
      CjGcxxTaskDO taskDO = cjGcxxTaskDao.findByTaskId(taskId);
      BeanUtils.copyProperties(taskDO, base);
      vo.setVo(base);
      // 藏品信息
//      vo.setBmlc(cjGcxxTaskDao.findNewByTaskId(taskId));
//      vo.setBmbm(cjGcxxTaskDao.findOldByTaskId(taskId));
      // 用户数据
      vo.setUser(user);
      // 任务定义
      vo.setTaskDefKey(taskDefKey);
      vo.setStatus(RenderStatus.SUCCESS);
    } catch (Exception e) {
      logger.error(e);
      vo.setStatus(RenderStatus.FAILED);
    }
    return vo;
  }
}
